#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
struct node {
	int num;
	int count;
};
int deleteAndEarn(vector<int>& nums) {



	sort(nums.begin(), nums.end());
	

	vector<node> nodes;
	int len = nums.size();
	int last=nums[0];
	node n1;
	n1.num = last;
	n1.count = 1;
	nodes.push_back( n1);
	int j = 0;
	for (int i = 1; i < len; i++) {
		if (nums[i] == last) {
			nodes[j].count++;
		}
		else {
			j++;
			n1.num = nums[i];
			n1.count = 1;
			nodes.push_back(n1);
			last = nums[i];
		}
	}
	


	int l2 = nodes.size();
	if (l2 == 1) {
		return nodes[0].num * nodes[0].count;
	}
	int* arr = new int[l2];
	arr[0] = nodes[0].num * nodes[0].count;
	if (nodes[0].num - nodes[1].num==-1) {
		arr[1] = max(arr[0], nodes[1].num * nodes[1].count);
	}
	else {
		arr[1] = arr[0] + nodes[1].num * nodes[1].count;
	}
	for (int i = 2; i < l2; i++) {
		if (nodes[i].num - nodes[i - 1].num == 1) {
			arr[i] = max(arr[i - 1], arr[i - 2] + nodes[i].num * nodes[i].count);
			
		}
		else {
			arr[i] = arr[i - 1] + arr[i - 2] + nodes[i].num * nodes[i].count;
		}
	}
	int res = arr[l2 - 1];
	//for (int l = 0; l < l2;l++) {
	//	cout << arr[l] << " ";
	//}
	//return 0;

	delete[]arr;
	return res;


}
